/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.extns.multimedia;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Item;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.GUIControl;
import javax.microedition.media.control.VideoControl;

//tbd
public final class CameraUtil {

	static Player player;

	static VideoControl vc;

	public static Item setupCamera() {
		Item item = null;
		try {

			player = Manager.createPlayer("capture://video");
			player.realize();
			player.start();

			if ((vc = (VideoControl) player.getControl("VideoControl")) != null) {
				item = (Item) vc.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE, "javax.microedition.lcdui.Item");
				if (item == null) {
					System.out.println("*********ITEM IS NULL***********");
				}
				// Item item = (Item)
				// vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);
				vc.setVisible(true);
			} else {
				System.out.println("video control is null");
			}

			// = (VideoControl) player.getControl("VideoControl");
			// vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
			// vc.setDisplayLocation(2, 2);
			// String imageType =
			// "encoding=jpeg&width=1600&height=1200&quality=superfine";
			// rn data;
			return item;
		} catch (IOException e) {
			System.out.println("I/O Exception" + e.toString());
			e.printStackTrace();
			// return null;

		} catch (MediaException e) {
			System.out.println("Media Exception " + e.toString());
			e.printStackTrace();
			// return null;
		}
		return item;
	}

	public static void setUpPlayer(Canvas canvas) {
		try {
			Player player = Manager.createPlayer("capture://video");
			player.realize();
			player.start();
			VideoControl vc;
			if ((vc = (VideoControl) player.getControl("VideoControl")) != null) {
				vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
				// Item item = (Item)
				// vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);
				vc.setVisible(true);
			} else {
				System.out.println("video control is null");
			}

			// = (VideoControl) player.getControl("VideoControl");
			// vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
			// vc.setDisplayLocation(2, 2);
			// String imageType =
			// "encoding=jpeg&width=1600&height=1200&quality=superfine";
			// rn data;
		} catch (IOException e) {
			System.out.println("I/O Exception" + e.toString());
			e.printStackTrace();
			// return null;

		} catch (MediaException e) {
			System.out.println("Media Exception " + e.toString());
			e.printStackTrace();
			// return null;
		}

	}

	public static byte[] takePic() {
		// System.out.println(System.getProperty("video.snapshot.encodings"));
		try {
			// encoding=jpeg&width=640&height=480&quality=normal?
			// String imageType =
			// "encoding=jpeg&width=1600&height=1200&quality=superfine";
			byte[] data = vc.getSnapshot("encoding=jpeg&width=640&height=480&quality=normal");
			return data;
			// if (data != null) {
			// System.out.println("Length " + data.length);
			// } else {
			// System.out.println("Data is NULL");
			// }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Excception " + e.toString());
			return null;
		}// imageType);
	}

}
